<template>
  <div class="home">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="logo">🔬 化学数据在线分析系统</div>
      <div class="user-menu" @click.stop="toggleDropdown">
        <img
          src="https://avatars.githubusercontent.com/u/9919?s=280&v=4"
          alt="用户头像"
          class="avatar"
        />
        <transition name="fade">
          <div v-if="showDropdown" class="dropdown" @click.stop>
            <ul>
              <li @click="goProfile">个人信息</li>
              <li @click="logout">注销登录</li>
            </ul>
          </div>
        </transition>
      </div>
    </header>

    <!-- 主体内容 -->
    <main class="content">
      <transition-group name="slide-up" tag="div" class="module-grid">
        <div
          v-for="(item, index) in modules"
          :key="item.name"
          class="module-card"
          @click="goTo(item.route)"
          :style="{ animationDelay: `${index * 0.1}s` }"
        >
          <div class="icon">{{ item.icon }}</div>
          <h2>{{ item.title }}</h2>
          <p>{{ item.desc }}</p>
        </div>
      </transition-group>
    </main>

    <!-- 页脚备案信息 -->
    <footer class="footer">
      <p>© 2025 化学数据在线分析系统 | 京ICP备20250001号</p>
    </footer>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const showDropdown = ref(false)

const modules = [
  {
    name: 'analysis',
    title: '数据分析',
    desc: '对实验数据进行可视化分析与模型预测',
    route: '/analysis',
    icon: '📊'
  },
  {
    name: 'processing',
    title: '数据处理',
    desc: '实现数据清洗、格式转换与统计计算',
    route: '/processing',
    icon: '⚙️'
  },
  {
    name: 'usage',
    title: '数据使用',
    desc: '支持数据共享、下载与在线报告',
    route: '/usage',
    icon: '🧪'
  }
]

function toggleDropdown() {
  showDropdown.value = !showDropdown.value
}
function logout() {
  alert('已注销登录')
  router.push('/login')
}
function goProfile() {
  router.push('/profile')
}
function goTo(route) {
  router.push(route)
}

// 点击外部关闭下拉菜单
function handleClickOutside(e) {
  if (!e.target.closest('.user-menu')) {
    showDropdown.value = false
  }
}
onMounted(() => document.addEventListener('click', handleClickOutside))
onBeforeUnmount(() => document.removeEventListener('click', handleClickOutside))
</script>

<style scoped>
/* 全局布局 */
.home {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background: linear-gradient(135deg, #eef2ff 0%, #dbeafe 50%, #eff6ff 100%);
  overflow: hidden;
  font-family: 'Microsoft YaHei', sans-serif;
}

/* 顶部导航栏 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 65px;
  background: linear-gradient(90deg, #2563eb, #3b82f6);
  color: white;
  padding: 0 30px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.15);
  position: relative;
  z-index: 100;
}
.logo {
  font-size: 22px;
  font-weight: bold;
  letter-spacing: 1px;
}
.user-menu {
  position: relative;
  cursor: pointer;
}
.avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 2px solid #fff;
  transition: transform 0.3s ease;
}
.avatar:hover {
  transform: scale(1.1);
}

/* 下拉菜单 */
.dropdown {
  position: absolute;
  right: 0;
  top: 55px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  width: 160px;
  animation: dropdownIn 0.3s ease;
}
.dropdown ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
.dropdown li {
  padding: 12px 18px;
  cursor: pointer;
  color: #333;
  transition: background 0.2s ease;
}
.dropdown li:hover {
  background: #f3f4f6;
}

/* 主体区域 */
.content {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow-y: auto;
}
.module-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 40px;
  justify-content: center;
  align-items: center;
  padding: 40px;
}

/* 模块卡片 */
.module-card {
  background: white;
  border-radius: 18px;
  width: 280px;
  height: 200px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  text-align: center;
  padding: 30px;
  cursor: pointer;
  transition: all 0.35s ease;
  animation: fadeInUp 0.6s ease both;
}
.module-card:hover {
  transform: translateY(-10px) scale(1.03);
  box-shadow: 0 12px 30px rgba(59, 130, 246, 0.3);
}
.icon {
  font-size: 42px;
  margin-bottom: 12px;
}
.module-card h2 {
  color: #2563eb;
  font-size: 20px;
  margin-bottom: 8px;
}
.module-card p {
  color: #555;
  font-size: 14px;
  line-height: 1.6;
}

/* 动画效果 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(25px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
@keyframes dropdownIn {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 页脚备案 */
.footer {
  background: #f3f4f6;
  text-align: center;
  padding: 12px 0;
  font-size: 14px;
  color: #666;
  border-top: 1px solid #e5e7eb;
}
</style>
